package com.doform.android.xianggucheng.activity.downorder;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.activity.BaseTitleActivity;
import com.doform.android.xianggucheng.bean.AddressBean;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.utils.Constans;
import com.doform.android.xianggucheng.utils.RexUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017/6/5.
 * GitHub：https://github.com/cxydxpx
 */

public class NewAddressActivity extends BaseTitleActivity {

    @BindView(R.id.et_name)
    EditText et_name;

    @BindView(R.id.et_address)
    EditText et_address;

    @BindView(R.id.et_phone)
    EditText et_phone;

    private String mName;
    private String mAddress;
    private String mPhone;

    private String mClientId;
    private String mClientGuid;


    @Override
    protected int layoutResId() {
        return R.layout.activity_new_add_address;
    }

    @Override
    protected void init() {
        String clientId = getIntent().getStringExtra(Constans.EXTRA_ADD_ADDRESS_ID);
        String clientGuid = getIntent().getStringExtra(Constans.EXTRA_ADD_ADDRESS_GUID);
        this.mClientId = clientId;
        this.mClientGuid = clientGuid;
    }

    @Override
    protected void showView() {
        titleBar.setTextTitle("新增收货地址");
    }

    @OnClick(R.id.btn_submit)
    public void click() {
        beforeSubmit();
    }

    private void beforeSubmit() {
        mName = et_name.getText().toString().trim();
        mAddress = et_address.getText().toString().trim();
        mPhone = et_phone.getText().toString().trim();

        if (TextUtils.isEmpty(mName)) {
            Toast.makeText(this, "请输入联系人", Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(mAddress)) {
            Toast.makeText(this, "请输入联系人地址", Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(mPhone) || RexUtils.isMobileNO(mPhone)) {
            Toast.makeText(this, "联系人电话格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        postDataToService(mName, mAddress, mPhone);
    }

    private void postDataToService(String mName, String mAddress, String mPhone) {

        HttpManager.instance()
                .addAddress(mClientId, mClientGuid, mName, mAddress, mPhone)
                .subscribe(new ErrorHandleSubscriber<AddressBean>() {
                    @Override
                    public void onNext(@NonNull AddressBean bean) {
                        Intent intent = new Intent(NewAddressActivity.this, SubmitOrderActivity.class);
                        intent.putExtra(Constans.EXTRA_SUBMIT_ORDER, bean);
                        intent.putExtra(Constans.EXTRA_SUBMIT_ORDER_CLIENT_ID, mClientId);
                        startActivity(intent);
                        finish();
                    }
                });
    }
}
